﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using Gios.Pdf;
using org.in2bits.MyXls;
using System.Drawing;

namespace SIMSProject.Common
{
    class Utils
    {
        #region Export report

        public static Stream ExportToPdf(DataTable table, string title, int[] columnWidth, string createdDate, string createdBy)
        {
            // Create pdf document
            PdfDocument myPdfDocument = new PdfDocument(PdfDocumentFormat.InCentimeters(21, 29.7));
            PdfTable myPdfTable = myPdfDocument.NewTable(new System.Drawing.Font("Verdana", 10), table.Rows.Count, table.Columns.Count, 2);
            myPdfTable.ImportDataTable(table);

            // Style pdf table
            myPdfTable.HeadersRow.SetColors(System.Drawing.Color.White, System.Drawing.Color.Navy);
            myPdfTable.SetColors(System.Drawing.Color.Black, System.Drawing.Color.White, System.Drawing.Color.Gainsboro);
            myPdfTable.SetBorders(System.Drawing.Color.Black, 1, BorderType.CompleteGrid);

            myPdfTable.SetColumnsWidth(columnWidth);
            myPdfTable.SetContentAlignment(ContentAlignment.TopLeft);

            int i = 0;
            // Here we start the loop to generate the table...
            while (!myPdfTable.AllTablePagesCreated)
            {
                // we create a new page to put the generation of the new TablePage:
                PdfPage newPdfPage = myPdfDocument.NewPage();
                PdfTablePage newPdfTablePage =
                    myPdfTable.CreateTablePage(new PdfArea(myPdfDocument, 48, 80, 500, 670));
                newPdfPage.Add(newPdfTablePage);
                if (i == 0)
                {
                    // we also put a Label 
                    var pta = new PdfTextArea(new System.Drawing.Font("Verdana", 15, FontStyle.Bold), System.Drawing.Color.Black
                                              , new PdfArea(myPdfDocument, 45, 20, 595, 50), ContentAlignment.TopLeft,
                                              title);
                    var ptaCD = new PdfTextArea(new System.Drawing.Font("Verdana", 10, FontStyle.Regular), System.Drawing.Color.Black
                                                , new PdfArea(myPdfDocument, 45, 45, 595, 50), ContentAlignment.TopLeft,
                                                createdDate);
                    var ptaCB = new PdfTextArea(new System.Drawing.Font("Verdana", 10, FontStyle.Regular), System.Drawing.Color.Black
                                                , new PdfArea(myPdfDocument, 45, 60, 595, 50), ContentAlignment.TopLeft,
                                                createdBy);
                    newPdfPage.Add(pta);
                    newPdfPage.Add(ptaCD);
                    newPdfPage.Add(ptaCB);
                }
                newPdfPage.SaveToDocument();
                i++;
            }

            Stream file = new MemoryStream();
            myPdfDocument.SaveToStream(file);
            return file;
        }

        public static XlsDocument ExportToExcel(string fileName, string sheetName, DataTable table, string[] docHeader, string title, string createdDate, string createdBy)
        {
            XlsDocument doc = new XlsDocument();
            doc.FileName = fileName;
            int rowMin, rowCount, colMin, colCount;

            rowMin = 3;
            rowCount = table.Rows.Count;

            colMin = 1;
            colCount = table.Columns.Count;

            if (docHeader.Length != colCount)
                return doc;

            if (rowCount > 65535) rowCount = 65535;
            if (rowCount < 0) rowCount = 0;
            if (rowMin < 1) rowMin = 1;
            if (rowMin > 32767) rowMin = 32767;

            if (colCount > 255) colCount = 255;
            if (colCount < 1) colCount = 1;
            if (colMin < 1) colMin = 1;
            if (colMin > 100) colMin = 100;

            if (sheetName.Length > 35) sheetName = sheetName.Substring(0, 35);

            Worksheet sheet = doc.Workbook.Worksheets.Add(sheetName);
            Cells cells = sheet.Cells;

            // Add title
            Cell cellTitle = cells.Add(1, 1, title + " Exported on " + DateTime.Now.ToString("dd/MM/yyyy"));
            cellTitle.Font.Weight = FontWeight.Bold;
            cellTitle.Font.Height = 300;

            for (int row = 0; row <= rowCount; row++)
            {
                if (row == 0)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        string colName = docHeader[col - 1]; //dt.Columns[col-1].ToString();
                        Cell cell = cells.Add(rowMin + row, colMin + col - 1, colName);

                        cell.TopLineStyle = 2;
                        cell.TopLineColor = Colors.Black;
                        cell.BottomLineStyle = 2;
                        cell.BottomLineColor = Colors.Black;
                        if (col == 1)
                        {
                            cell.LeftLineStyle = 2;
                            cell.LeftLineColor = Colors.Black;
                        }
                        cell.RightLineStyle = 2;
                        cell.RightLineColor = Colors.Black;

                        cell.Font.Weight = FontWeight.Bold;
                        cell.Pattern = 1;
                        cell.PatternColor = Colors.Silver;
                    }
                }
                else
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        string value = table.Rows[row - 1][col - 1] == null
                                           ? ""
                                           : table.Rows[row - 1][col - 1].ToString();
                        Cell cell = cells.Add(rowMin + row, colMin + col - 1, value);
                    }
                }
            }
            return doc;
        }
        #endregion
    }
}
